/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service.query;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import info.peigen.hotpot.business.account.core.domain.account.action.AccountConvertor;
import info.peigen.hotpot.business.account.core.repository.entity.AccountLogEntity;
import info.peigen.hotpot.business.account.facade.QueryAccountLogServiceFacade;
import info.peigen.hotpot.business.account.facade.info.AccountLogInfo;
import info.peigen.hotpot.business.account.facade.order.query.QueryAccountLogPageOrder;
import info.peigen.hotpot.business.account.facade.result.query.QueryPageAccountLogResult;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.service.base.ServiceQuery;
import info.peigen.hotpot.common.service.base.ServiceQueryContext;
import info.peigen.hotpot.component.data.repository.beetlsql.util.BeetlSqlPageResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(QueryAccountLogService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/12/10
 */
@SuppressWarnings("ALL")
@Slf4j
@Component
public class QueryAccountLogService extends AbstractAccountQueryService implements QueryAccountLogServiceFacade {

    @Override
    public QueryPageAccountLogResult queryAccountLogPage(QueryAccountLogPageOrder order) {
        ServiceQueryContext serviceContext = new ServiceQueryContext().setOrder(order).setResult(new QueryPageAccountLogResult());

        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                // 1. 查 Entity
                // 1.1 构造分页
                org.beetl.sql.core.page.PageResult<AccountLogEntity> accountLogEntityPage = accountLogRepository.getMapper().createLambdaQuery()
                        .andEq(AccountLogEntity::getAccountId, Query.filterEmpty(order.getAccountId()))
                        .andEq(AccountLogEntity::getTransCode, Query.filterNull(order.getTransCode()))
                        .andEq(AccountLogEntity::getDirection, Query.filterNull(order.getDirection()))
                        .desc(AccountLogEntity::getCreateTime)
                        .page(order.getPageNum(), order.getPageSize());
                List<AccountLogEntity> entityList = accountLogEntityPage.getList();

                serviceContext.setEntities(entityList);
                serviceContext.setPage(BeetlSqlPageResultUtil.beetlSqlPageResultFromSuccess(accountLogEntityPage));
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                // 3. 转换 Info
                List<AccountLogEntity> entityList      = serviceContext.getEntities();
                List<AccountLogInfo>   accountInfoList = new ArrayList<>();
                if (!CollectionUtil.isEmpty(entityList)) {
                    accountInfoList = entityList.stream().map(entity -> AccountConvertor.convertAccountLogInfo(AccountConvertor.convertAccountLog(entity))).collect(Collectors.toList());
                }

                PageResult<AccountLogEntity> accountLogEntityPage = serviceContext.getPage();
                PageResult<AccountLogInfo>   accountLogInfoPage   = BeanUtil.copyProperties(accountLogEntityPage, PageResult.class);
                ((QueryPageAccountLogResult) serviceContext.getResult()).fromSuccess(accountLogInfoPage.getInfo());
            }
        });
        return (QueryPageAccountLogResult) serviceContext.getResult();
    }
}